﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ActivitiesControl.Domain.Model;
using ActivitiesControl.Domain.DAL;
using System.Data.SqlClient;
using System.Data;
using ActivitiesControl.Domain.Model.Entities;

namespace ActivitiesControl.Repository.DAL.SqlServer
{
    internal class SqlServerUserRepository
        : SqlServerBaseRepository<User>, IUserRepository
    {

        public User GetUserByLoginPassword(string login, string password)
        {
            try
            {
                Connection.Open();
                SqlCommand cmd = Connection.CreateCommand();
                cmd.CommandText = "select * from Resources where username = @username and password = @password";
                cmd.Parameters.AddWithValue("username", login);
                cmd.Parameters.AddWithValue("password", password);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataTable result = new DataTable();
                adapter.Fill(result);

                if (result != null && result.Rows != null && result.Rows.Count > 0)
                    return Row2Object(result.Rows[0]);

                return null;
            }
            finally
            {
                Connection.Close();
            }
        }

		public User GetUserByLogin(string login)
		{
			try
			{
				Connection.Open();
				SqlCommand cmd = Connection.CreateCommand();
				cmd.CommandText = "select * from Resources where username = @username";
				cmd.Parameters.AddWithValue("username", login);
				SqlDataAdapter adapter = new SqlDataAdapter(cmd);
				DataTable result = new DataTable();
				adapter.Fill(result);

				if (result != null && result.Rows != null && result.Rows.Count > 0)
					return Row2Object(result.Rows[0]);

				return null;
			}
			finally
			{
				Connection.Close();
			}
		}

        public void Add(User entity)
        {
            throw new NotImplementedException();
        }

        public void Update(User entity)
        {
            try
            {
                Connection.Open();
                SqlCommand cmd = Connection.CreateCommand();

                cmd.CommandText = "UPDATE Resources set password = @password where resourceId = @resourceId";

                cmd.Parameters.AddWithValue("resourceId", entity.Id);
                cmd.Parameters.AddWithValue("password", entity.Password);
                cmd.ExecuteNonQuery();
            }
            finally
            {
                Connection.Close();
            }
        }

        public void Remove(User entity)
        {
            throw new NotImplementedException();
        }

		public void RemoveById(int id)
		{
			throw new NotImplementedException();
		}

        public User GetById(int id)
        {
            try
            {
                Connection.Open();
                SqlCommand cmd = Connection.CreateCommand();
                cmd.CommandText = "select * from Resources where resourceId = @resourceId";
                cmd.Parameters.AddWithValue("resourceId", id);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataTable result = new DataTable();
                adapter.Fill(result);

                if (result != null && result.Rows != null && result.Rows.Count > 0)
                    return Row2Object(result.Rows[0]);

                return null;
            }
            finally
            {
                Connection.Close();
            }
        }

        public IEnumerable<User> GetAll()
        {
            throw new NotImplementedException();
        }

        protected override User Row2Object(DataRow row)
        {
            return new User
            {
                Id = Convert.ToInt32(row["resourceId"]),
                Username = row["username"].ToString(),
                ResourceId = Convert.ToInt32(row["resourceId"]),
				Resource = new Resource 
				{ 
					Id = Convert.ToInt32(row["resourceId"]), 
					Name = row["name"].ToString() 
				},
				UserType = (UserType)Convert.ToInt32(row["userType"])
            };
        }
	}
}